package br.sed.theenemy.presentation.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.sed.theenemy.model.bean.User;
import br.sed.theenemy.presentation.UserSession;
import br.sed.theenemy.presentation.form.HomeForm;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 * 
 */
public class HomeAction extends DispatchAction {

	// forwards
    private static final String HOME_PAGE = "HomePage";
    private static final String LOGIN_PAGE = "LoginPage";
    
	public ActionForward unspecified(final ActionMapping mapping,
	           final ActionForm form, final HttpServletRequest request, 
	           final HttpServletResponse response) {
		HomeForm homeForm = (HomeForm) form;
		
		final UserSession session = new UserSession(request, false);
        final User userLogged = session.getLoggedUser();
        
        if (userLogged == null) {
            // User not logged in, redirect to login.jsp
            return mapping.findForward(LOGIN_PAGE);
        }
        
    	homeForm.setId(userLogged.getId());
    	homeForm.setFirstName(userLogged.getFirstName());
    	homeForm.setLastName(userLogged.getLastName());
    	homeForm.setProfileImg(userLogged.getImgUrl());
		
        // User logged redirect to home
        return mapping.findForward(HOME_PAGE);
        
	}
}
